#include "config.h"
#include "debug.h"
#include "state.h"


uint16_t state::next_id = 0;
state* state::curr_state = NULL;
state* state::next_state = NULL;
state* state::prev_state = NULL;


state::state() 
: id(next_id++) 
{
}


state::~state()
{
}


void state::set_next(state* s)
{
   next_state = s;
}


void state::update()
{
   if (curr_state != NULL) {
      curr_state->on_update();

      if (next_state != NULL && next_state->id != curr_state->id) {
         prev_state = curr_state;
         curr_state = next_state;
         next_state = NULL;

         prev_state->on_leave();
         curr_state->on_enter();
      }
   }
   else if (next_state != NULL) {
      curr_state = next_state;
      next_state = NULL;

      curr_state->on_enter();
   }
}

